import { defineStore } from 'pinia';
import { store } from '@/store';

interface AuthResourceState {
	// 统一资源数据
	resourceData: any[];
	// 菜单数据
	menuData: any[];
	// 是否开启权限认证
	enablePermissionValid: boolean;
	// 实体权限数据
	srfappdeData: any;
}

const useAuthResourceStore = defineStore({
	id: 'authResource',
	state: (): AuthResourceState => ({
		resourceData: [],
		menuData: [],
		enablePermissionValid: false,
		srfappdeData: {},
	}),
	getters: {
		getEnablePermissionValid(): boolean {
			return this.enablePermissionValid;
		},
		getMenuData(): any[] {
			return this.menuData;
		},
		getResourceData(): any[] {
			return this.resourceData;
		},
	},
	actions: {
		setResourceData(resourceArray: any[]) {
			if (!resourceArray) {
				return;
			}
			this.resourceData = resourceArray;
		},
		setMenuData(menuData: any[]) {
			if (!menuData) {
				return;
			}
			this.menuData = menuData;
		},
		setEnablePermissionValid(enablePermissionValid: boolean) {
			this.enablePermissionValid = enablePermissionValid;
		},
		commitAuthData({
			unires,
			appmenu,
			enablepermissionvalid,
		}: {
			unires: Array<any>;
			appmenu: Array<any>;
			enablepermissionvalid: boolean;
		}) {
			this.setResourceData(unires && unires.length > 0 ? unires : []);
			this.setMenuData(appmenu && appmenu.length > 0 ? appmenu : []);
			this.setEnablePermissionValid(enablepermissionvalid === true);
		},
		clearAuthData() {
			this.resourceData = [];
			this.menuData = [];
			this.enablePermissionValid = false;
		},
	},
});

export function useAuthResourceStoreWithOut() {
	return useAuthResourceStore(store);
}
